﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using UserData.Data;

namespace UserData.Controllers
{
    [ApiController]
    [Route("api/user")]
    
    public class UserController(IuserManager userManager, IOptions<DemoOption> optionAccessor) : ControllerBase
    {
        [HttpPost]
        public Task<UserDto> Create(UserCreateDto dto)
        {
            var option = optionAccessor.Value;
            if (dto.Password.Length < option.PasswordMinLength)
            {
                throw new Exception($"密码长度不能小于{option.PasswordMinLength}位");
            }
            if (option.Forbidden.Contains(dto.Name))
            {
                throw new Exception("用户名不合法");
            }
            return userManager.CreateAsync(dto, HttpContext.RequestAborted);
        }
        [HttpGet]
        public Task<List<UserDto?>> Get(int id)
        {
             return userManager.GetAsync(id);
           
        }
    }
}
